<?php

namespace Model\Room;

use Extension\Tools;
use Model\User;
use Handler\ZoneHandler;

class ClientRequest {
	
	/**
	 *
	 * @var RoomMain
	 */
	private $m_RoomMain;
	/**
	 *
	 * @var Algorithm
	 */
	private $m_Algo;
	/**
	 * 构造函数
	 *
	 * @param RoomMain $roomMain        	
	 * @param Algorithm $algo        	
	 */
	function __construct($roomMain, $algo) {
		$this->m_RoomMain = $roomMain;
		$this->m_Algo = $algo;
	}
	/**
	 * 析构函数
	 */
	function __destruct() {
	}
	/**
	 * 测试请求
	 *
	 * @param unknown $user        	
	 * @param unknown $json        	
	 */
	public function mb_TestRequest($user, $json) {
		Tools::mb_LogResult ( "测试请求" . json_encode ( $json ) );
	}
	/**
	 *
	 * @param User $user        	
	 * @param unknown $json        	
	 */
	public function mb_GetSyncData($user, $json) {
		$recNo = $json->recno;
		$this->m_RoomMain->mb_DataOut ( $user, $recNo, ErrCode_Normal, json_encode ( $this->m_RoomMain->mb_GetSyncData () ) );
	}
	/**
	 * 准备
	 *
	 * @param unknown $user        	
	 * @param unknown $json        	
	 */
	public function mb_Ready($user, $json) {
		$recNo = $json->recno;
		$this->m_Algo->mb_GetPlayerForName ( $user->m_Name )->m_IsReady = true;
	}
	/**
	 * 抢庄
	 *
	 * @param unknown $user        	
	 * @param unknown $json        	
	 */
	public function mb_QiangZhuang($user, $json) {
		$recNo = $json->recno;
		$bankerMul = $json->qiangzhuang;
		
		if ($this->m_RoomMain->m_CurStatus != RoomMain::STATUS_QIANGZHUANG)
			return;
		
		$this->m_Algo->mb_QiangZhuang ( $user, $bankerMul );
	}
	/**
	 * 下注
	 *
	 * @param unknown $user        	
	 * @param unknown $json        	
	 */
	public function mb_DownBet($user, $json) {
		$recNo = $json->recno;
		$playerMul = $json->downbet;
		
		if ($this->m_RoomMain->m_CurStatus != RoomMain::STATUS_DOWNBET)
			return;
		
		$this->m_Algo->mb_DownBet ( $user, $playerMul );
	}
	/**
	 * 添加一个机器人
	 *
	 * @param unknown $user        	
	 * @param unknown $json        	
	 */
	public function mb_AddRobot($user, $json) {
		$recNo = $json->recno;
		ZoneHandler::mb_GetInstance ()->mb_RobotEnterRoom ( $this->m_RoomMain->m_RoomId );
		$this->m_RoomMain->mb_DataOut ( $user, $recNo, ErrCode_Normal, ErrMsg_Normal );
	}
	/**
	 *
	 * @param unknown $user        	
	 * @param unknown $json        	
	 */
	public function mb_RemoveRobot($user, $json) {
		$recNo = $json->recno;
		$robot = null;
		$players = $this->m_Algo->mb_GetAllPlayers ();
		for($i = 0; $i < count ( $players ); $i ++) {
			if (isset ( $players [$i] ) && $players [$i]->m_User->m_IsRobot == true) {
				$robot = $players [$i];
				break;
			}
		}
		if ($robot != null)
			ZoneHandler::mb_GetInstance ()->mb_RobotExitRoom ( $this->m_RoomMain->m_RoomId, $robot->m_User );
	}
	/**
	 * 设置牌型最高者
	 *
	 * @param unknown $user        	
	 * @param unknown $json        	
	 */
	public function mb_SetCardTypeWinner($user, $json) {
		$recNo = $json->recno;
		$name = $json->name;
		
		if ($this->m_RoomMain->m_CurStatus == RoomMain::STATUS_QIANGZHUANG || $this->m_RoomMain->m_CurStatus == RoomMain::STATUS_DOWNBET) {
			$this->m_Algo->mb_SetCardTypeWinner ( $name );
			$this->m_RoomMain->mb_DataOut ( $user, $recNo, ErrCode_Normal, '设置成功' );
		} else {
			$this->m_RoomMain->mb_DataOut ( $user, $recNo, ErrCode_Toast, '当前状态不允许，请在抢庄状态或下注状态再试' );
		}
	}
	/**
	 * 开牌
	 *
	 * @param unknown $user        	
	 * @param unknown $json        	
	 */
	public function mb_OpenPoker($user, $json) {
		$recNo = $json->recno;
		$this->m_Algo->mb_OpenPoker ( $user );
		$this->m_RoomMain->mb_DataOut ( $user, $recNo, ErrCode_Normal, ErrMsg_Normal );
	}
	/**
	 * 预设扑克牌
	 *
	 * @param User $user        	
	 * @param unknown $json        	
	 */
	public function mb_PresetPokers($user, $json) {
		$recNo = $json->recno;
		Tools::mb_LogResult("$user->m_Name 进行选牌！ ");
		$this->m_RoomMain->mb_DataOut ( $user, $recNo, ErrCode_Normal, ErrMsg_Normal );
		return ;
		

		$pokers = json_decode ( $json->pokers, true );
		$player = $this->m_Algo->mb_GetPlayerForName ( $user->m_Name );
		$player->m_PresetPokers = $pokers;
		$this->m_RoomMain->mb_DataOut ( $user, $recNo, ErrCode_Normal, ErrMsg_Normal );
	}
}